<template>
  <div class="role">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="角色名称">
            <el-input v-model="form.name" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :disabled="isloading" @click="onSubmit"
              >查询</el-button
            >
            <el-button @click="onReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-button @click="handleAdd">添加角色</el-button>
      <el-table v-loading="isloading" :data="roleList" style="width: 100%">
        <el-table-column prop="id" label="编号" width="180"> </el-table-column>
        <el-table-column prop="name" label="角色名称" width="180">
        </el-table-column>
        <el-table-column prop="description" label="描述" width="180">
        </el-table-column>
        <el-table-column width="150px" label="添加时间">
          <template slot-scope="scope">
            {{ scope.row.createdTime | dataFormat }}
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作" width="150px">
          <template slot-scope="scope">
            <div>
              <el-button type="text" @click="$router.push({name: 'alloc-menu', params: {roleId: scope.row.id} })">分配菜单</el-button>
              <el-button type="text">分配资源</el-button>
              <el-button type="text" @click="handleEdit(scope.row)"
                >编辑</el-button
              >
              <el-button type="text" @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="form.current"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="form.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalCount"
    >
    </el-pagination>
    <el-dialog :title="isEdit ? '编辑角色' : '添加角色'" :visible.sync="dialogFormVisible">
      <add-role @success='ishide' :role-id="roleId" :is-edit="isEdit" @cancel='onCancel'></add-role>
    </el-dialog>
  </div>
</template>

<script>
import addRole from './addRole'
import { getRoleAll, deleteRole } from '@/services/role'

function isaddZero (data) {
  return data >= 10 ? data : '0' + data
}

export default {
  name: 'roleList',
  data () {
    return {
      isloading: false,
      form: {
        name: '',
        size: 10,
        current: 0
      },
      roleList: [],
      totalCount: 0,
      dialogFormVisible: false,
      isEdit: false,
      roleId: ''
    }
  },
  components: {
    addRole
  },
  created () {
    this.getAllroleList()
  },
  methods: {
    handleAdd () {
      this.dialogFormVisible = true
      this.isEdit = false
    },
    onCancel () {
      this.dialogFormVisible = false
      this.$message.success('取消成功')
    },
    ishide (data) {
      this.dialogFormVisible = data
      this.getAllroleList()
    },
    async getAllroleList () {
      this.isloading = true
      const { data } = await getRoleAll(this.form)
      if (data.code === '000000') {
        this.roleList = data.data.records
        this.isloading = false
        this.totalCount = data.data.total
        if (!this.totalCount) {
          return this.$message.error('没有相关数据')
        }
      }
    },
    handleEdit (role) {
      this.dialogFormVisible = true
      this.isEdit = true
      this.roleId = role.id
    },
    handleDelete (role) {
      this.$confirm('是否确认删除')
        .then(async () => {
          await deleteRole(role.id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getAllroleList()
        })
        .catch((error) => {
          if (error && error.response) {
            this.$message.error('删除失败，请重试')
          } else {
            this.$message.error('已取消删除')
          }
        })
    },
    handleSizeChange (val) {
      this.form.size = val
      this.current = 0
      this.getAllroleList()
    },
    handleCurrentChange (val) {
      this.form.current = val
      this.getAllroleList()
    },
    async onSubmit () {
      this.getAllroleList()
    },
    onReset () {
      this.form.name = ''
      this.getAllroleList()
    }
  },
  filters: {
    dataFormat (data) {
      data = new Date(data)
      return `${data.getFullYear()}-${isaddZero(
        data.getMonth() + 1
      )}-${isaddZero(data.getDate())}

            ${isaddZero(data.getHours())}:${isaddZero(
        data.getMinutes()
      )}:${isaddZero(data.getSeconds())}`
    }
  }
}
</script>

<style>
</style>
